package com.miracle.mango.controller;

import com.miracle.mango.entity.user.User;
import com.miracle.mango.entity.user.UserMapper;
import com.miracle.mango.entity.user.UserServive;
import com.miracle.mango.util.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: wangxf
 * @date: Created in 2020/8/23 12:27
 */
@RestController
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserServive userServive;

    @ApiOperation(value = "获取用户详情")
    @GetMapping("/user/{id}")
    public ResultBody getById(@PathVariable("id") String id) {
        User user = userMapper.getById(id);
        return new ResultBody<User>(user);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("/user")
    public ResultBody save(User user) {
        user = new User();
        user.setName("哈哈哈").setUsername("aaa").setPassword("123456");
        if (userServive.save(user) == 1) {
            return new ResultBody("用户添加成功！", user);
        }
        return new ResultBody("500", "用户添加失败，服务器内部错误");
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("/user")
    public ResultBody update(User user) {
        user = new User();
        user.setName("哈哈哈修改后").setUsername("aaabbb").setPassword("123456bbb");
        if (userServive.update(user) == 1) {
            return new ResultBody("用户更新成功！", user);
        }
        return new ResultBody("500", "用户更新失败，服务器内部错误");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/user/{id}")
    public ResultBody remove(@PathVariable("id") String id) {
        if (userServive.removeById(id) == 1) {
            return new ResultBody("用户删除成功！");
        }
        return new ResultBody("500", "用户删除，服务器内部错误");
    }

}
